<?php
class WebPageTagModel extends Model {
	var $_table = 'web_page_tag';
	
	var $page_id_nr; // INT UNSIGNED NOT NULL,
	var $tag_id_nr; // INT UNSIGNED NOT NULL,
	

	/**
	 * 
	 * 设置页面标签
	 * @param int $page_id
	 * @param array $tags ('标签1','标签2')
	 */
	public function setTags($page_id, $tags) {
		$tM = new WebTagModel ();
		$htags = $tM->getTagsInTags ( $tags );
		$data = array ();
		foreach ( $htags as $tag_id => $tag ) {
			$data [] = "($page_id,$tag_id)";
		}
		$rst = true;
		if (! empty ( $data )) {
			$fname = $this->fullname;
			$sql = "INSERT INTO `{$fname}` (page_id,tag_id) VALUES " . join ( ',', $data );
			$rst = $this->execute ( $sql );
		}
		return $rst;
	}
	/**
	 * 
	 * 设置页面标记
	 * @param int $page_id
	 * @param array $flags (1,2,3)
	 */
	public function setFlags($page_id, $flags) {
		$where ['page_id'] = $page_id;
		$data = array ();
		$this->delete ( $where );
		foreach ( $flags as $fid ) {
			if (is_numeric ( $fid )) {
				$data [] = "($page_id,$fid)";
			}
		}
		$rst = true;
		if (! empty ( $data )) {
			$fname = $this->fullname;
			$sql = "INSERT INTO `{$fname}` (page_id,tag_id) VALUES " . join ( ',', $data );
			$rst = $this->execute ( $sql );
		}
		return $rst;
	}
	/**
	 * 
	 * 获取页面的标签
	 * @param int $page_id
	 */
	public function getPageTags($page_id) {
		$this->alias ( 'WPT' );
		$this->join ( "web_tag as WT", "WPT.tag_id = WT.tag_id" );
		return $this->where ( array ('WPT.page_id' => $page_id, 'WT.type' => 'tag' ) )->retrieve ( 'WT.tag_id,WT.tag' );
	}
	/**
	 * 
	 * 获取页面的标记
	 * @param int $page_id
	 */
	public function getPageFlags($page_id) {
		$this->alias ( 'WPT' );
		$this->join ( "web_tag as WT", "WPT.tag_id = WT.tag_id" );
		return $this->where ( array ('WPT.page_id' => $page_id, 'WT.type' => 'flag' ) )->retrieve ( 'WT.tag_id,WT.tag' );
	}
}